package com.dongdongshop.service.impl;

import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.w3c.dom.stylesheets.LinkStyle;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FreemarkerServiceImpl implements FreemarkerService {

    @DubboReference
    private TbGoodsService tbGoodsService;

    @DubboReference
    private TbGoodsDescService tbGoodsDescService;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @DubboReference
    private TbItemService tbItemService;

    @DubboReference
    private TbItemCatService tbItemCatService;

    @Value("${myfreemarker.ouputPath}")
    private String ouputPath;

    @Override
    public void createPage(Long goodsId) {

        FileWriter fileWriter =null;
        //1.创建configuration对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        try {
            //4.加载模板对象
            Template template = configuration.getTemplate("item.ftl");
            TbGoods tbGoods = tbGoodsService.selectGoodsById(goodsId);
            TbGoodsDesc tbGoodsDesc = tbGoodsDescService.selectGoodsById(goodsId);
            List<TbItem> tbItems = tbItemService.selectByGoodsId(goodsId);

            TbItemCat tbItemCat1 = tbItemCatService.selectById(tbGoods.getCategory1Id());
            TbItemCat tbItemCat2 = tbItemCatService.selectById(tbGoods.getCategory2Id());
            TbItemCat tbItemCat3 = tbItemCatService.selectById(tbGoods.getCategory3Id());


            //5.创建模板需要的数据 一般使用map和实体类对象都可
            Map map = new HashMap();
            map.put("tbGoods",tbGoods);
            map.put("tbGoodsDesc",tbGoodsDesc);
            map.put("tbItems",tbItems);

            map.put("tbItemCat1",tbItemCat1);
            map.put("tbItemCat2",tbItemCat2);
            map.put("tbItemCat3",tbItemCat3);
            map.put("name",tbGoods.getGoodsName());
            //6.创建输出流对象  静态页面要存放的位置  后缀写要输出的文件类型
            fileWriter = new FileWriter(ouputPath+goodsId+".html");
            //7.输出页面
            template.process(map,fileWriter);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileWriter != null) {
                //8.关流
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
